package com.feituan.manager.controller;


import com.alibaba.fastjson.JSON;
import com.feituan.core.persistence.service.IMerchantService;
import com.feituan.core.persistence.service.IProductService;
import com.feituan.core.pojo.entity.Merchant;
import com.feituan.core.pojo.entity.Product;
import com.feituan.core.pojo.request.*;
import com.feituan.core.pojo.response.MerchantProductRes;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.pojo.response.page.PageData;
import com.feituan.core.pojo.vo.Image;
import com.feituan.core.pojo.vo.Model;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaohu
 * @since 2024-05-09
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品管理")
public class ProductController extends BaseController{
    @Autowired
    private IProductService iProductService;
    @PostMapping("/list_page")
    @ApiOperation("")
    public Response<PageData<Product>> listPage(@Valid CommonPageReq request) {
        setDefaultPageParameters(request);
        return Response.success(iProductService.queryPage(request));
    }
    
    @PostMapping("/save")
    @ApiOperation("id为空新增，id不为空修改")
    public Response<Boolean> save(@Valid Product request) {
        Product coupon=new Product();
        BeanUtils.copyProperties(request,coupon);
        if(coupon.getId() == null){
            coupon.setCreateTime(Calendar.getInstance().getTime());
            return response(iProductService.save(coupon)) ;
        }else{
            coupon.setCreateTime(null);
            return response(iProductService.updateById(coupon));
        }
    }
    @PostMapping("/delete")
    @ApiOperation("")
    public Response<Boolean> delete(Long id) {
        return response(iProductService.removeById(id));
    }

    @PostMapping("/set_models")
    @ApiOperation("设置规格")
    public Response<Boolean> setModels(@RequestBody ProductSetModelReq entity) {
        if(entity.getId() != null){
            if(!CollectionUtils.isEmpty(entity.getModelList())){
                Product product = new Product();
                product.setId(entity.getId());
                product.setModels(JSON.toJSONString(entity.getModelList()));
                iProductService.updateById(product);
                return response(true);
            }


        }
        return response(false);
    }

    @PostMapping("/get_models")
    @ApiOperation("获取规格")
    public Response<List<Model>> getModels(Long id) {
        if(id != null){
            MerchantProductRes merchantProductRes=new MerchantProductRes();
            merchantProductRes.setId(id);
            Product product=iProductService.getById(id);
            if(product != null && !StringUtils.isEmpty(product.getModels())){
                List<Model> models = JSON.parseArray(product.getModels(),Model.class);
                return Response.success(models);
            }
        }
        return Response.success(new ArrayList<>());
    }

    @PostMapping("/get_detail_images")
    @ApiOperation("获取详情图片")
    public Response<List<Image>> getDetailImages(Long id) {
        if(id != null){
            Product product=iProductService.getById(id);
            if(product != null && !StringUtils.isEmpty(product.getImages())){
                List<Image> models = JSON.parseArray(product.getImages(),Image.class);
                models.sort(Comparator.comparing(Image::getIndex));
                return Response.success(models);
            }
        }
        return Response.success(new ArrayList<>());
    }
    @PostMapping("/set_detail_images")
    @ApiOperation("设置详情图片")
    public Response<Boolean> setDetailImages(@RequestBody ProductSetImageReq entity) {
        if(entity.getId() != null){
            if(!CollectionUtils.isEmpty(entity.getImageList())){
                Product product = new Product();
                product.setId(entity.getId());
                product.setImages(JSON.toJSONString(entity.getImageList()));
                iProductService.updateById(product);
                return response(true);
            }


        }
        return response(false);
    }
}
